<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" class="flex1" placeholder="请选择审核状态" style="width: 100%;">
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['user:project:edit']"
        >审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['user:project:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <div v-if="scope.row.status == 0">审核通过</div>
          <div v-if="scope.row.status == 1">审核中</div>
          <div v-if="scope.row.status == 2">审核拒绝</div>
          <div v-if="scope.row.status == 3"><span style="color: red;">已删除</span></div>
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="单价" align="center" prop="amount" />
      <el-table-column label="标签" align="center" prop="lable" />
      <el-table-column label="图片" align="center" prop="img">
        <template slot-scope="scope">
          <el-image style="width: 20px; height: 20px;margin-right: 5px;" :src="item"
            v-for="(item, index) in scope.row.img.split(',')" :key="index" @click="Screen(item)"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="语音" align="center" prop="voice" width="245">
        <template slot-scope="scope">
          <audio :src="scope.row.voice" controls style="width: 240px;"></audio>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['user:project:edit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['user:project:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改咨询师项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="简介" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="单价" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入单价" />
        </el-form-item>
       <el-form-item label="图片" prop="img">
         <div style="display: flex;align-items: center;flex-wrap: wrap;">
           <div style="position: relative;"  v-for="(item,index) in form.simpleSpectrum1" :key="index">
             <div style="position: absolute;
              top: 10px;
              right: 12px;
              color: rgb(240, 0, 0);
              background: #fff;
              width: 20px;
              line-height: 20px;
              text-align: center;
            height: 20px;" @click="handleRemove(index)">x</div>
             <img v-if="form.simpleSpectrum1.length > 0" :src="item" style="width: 100px;height: 100px;margin-right: 10px;margin-top: 10px;" class="avatar">
           </div>
           <el-upload class="avatar-uploader" action="" :http-request="ossUploadFunSpectrum" style="margin-top: 10px;" :show-file-list="false">
           	<i class="el-icon-plus avatar-uploader-icon" ></i>
           </el-upload>
         </div>
       </el-form-item>
       <el-form-item label="语音"  prop="voice">
        <el-upload class="avatar-uploader" action="" :http-request="ossUploadFunVoice" :show-file-list="false">
        	<i class="el-icon-plus avatar-uploader-icon" ></i>
        </el-upload>
        <audio controls v-if="form.voice" :src="form.voice" style="width: 300px;height: 60px;" class="avatar" >
          <source :src="item"  type="audio/mpeg">
          <source :src="item"  type="audio/ogg">
          Your browser does not support this audio format.
        </audio>
       </el-form-item>
       <el-form-item label="审核状态" prop="status">
         <el-select v-model="form.status" class="flex1" placeholder="请选择" style="width: 100%;">
           <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
           </el-option>
         </el-select>
       </el-form-item>
       <el-form-item label="拒绝理由" prop="rejected">
         <el-input v-model="form.rejected" type="textarea" placeholder="请输入内容" />
       </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
  </div>
</template>

<script>
import { listProject, getProject, delProject, addProject, updateProject } from "@/api/user/project";

export default {
  name: "Project",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 咨询师项目表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        nickName: null,
        title: null,
        amount: null,
        img: null,
        voice: null,
        lable: null,
        status: null,
      },
      dialogImageUrl: '',
      dialogVisible: false,
      // 表单参数
      form: {},
      statusOptions: [{
        value: 1,
        label: '审核中'
      }, {
        value: 0,
        label: '审核通过'
      }, {
        value: 2,
        label: '审核拒绝'
      }, {
        value: 3,
        label: '已删除'
      }],
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "单价不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "简介不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleRemove(index){
      this.form.simpleSpectrum1.splice(index,1);
    },
    Screen(e) {
      this.dialogImageUrl = e
      this.dialogVisible = true;
    },
    /** 查询咨询师项目列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        title: null,
        remark: null,
        amount: null,
        img: null,
        voice: null,
        lable: null,
        createTime: null,
        updateTime: null,
        rejected: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加咨询师项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProject(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改咨询师项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProject(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProject(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 图片上传
    ossUploadFunVoice(file) {
    	this.ossUpload(file.file).then(res => {
    		this.form.voice = res
    	})
    },
    // 上传图片
    ossUploadFunSpectrum(file){
        if(file.file.type.indexOf('audio') != -1 || file.file.type.indexOf('video') != -1) {
         return this.$alert('请选择图片格式文件', '提示', {
           confirmButtonText: '确定',
         });
        }
        this.ossUpload(file.file).then(res => {
          this.form.simpleSpectrum1.push(res);
        })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除咨询师项目编号为"' + ids + '"的数据项？').then(function() {
        return delProject(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/project/export', {
        ...this.queryParams
      }, `project_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
